<?php

namespace App\Console\Commands;

use App\Model\tel;
use Illuminate\Console\Command;

class callback extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'callback';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $key = 'hfUser';
        $secret = '055c7fbbf0021023adad7be38942c477';
        while(true){
            try {
                $between = [1,2];
                $tels = tel::where("callbackstatus",0)->whereBetween('isok',$between)->select("order","orderno","chargeaccount","isok",'callbackUrl',"oktime")->get();
                foreach ($tels as $tel){
                    $data = [
                        "order"     =>      $tel['order'],
                        "orderno"   =>      $tel['orderno'],
                        "chargeaccount"=>   $tel['chargeaccount'],
                        "order_status"=>    $tel['isok'],
                        "finish_time" =>    $tel['oktime']
                    ];
                    $str = implode("", $data);
                    $data['sign'] = md5($key.$str.$secret);
                    $url = $tel['callbackUrl'];
                    $res = curlPost($url,json_encode($data));
                    $info['callbackstatus'] = 1;
                    $info['status'] = '已处理';
                    $info['callback'] = $res;
                    tel::where("order",$tel['order'])->update($info);
                }
            }catch (\Exception $e){
                echo $e->getMessage();
            }
            sleep(3);
        }

    }
}
